package controllers;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import com.etaoking.commons.BeanUtilsEx;
import com.etaoking.commons.Contants;

import models.Comment;
import models.Orders;
import models.Users;

public class CommentAction extends AdminController{
	public static void index(){
		render();
	}
/**
 * 发表评论
 * @param orderId
 * @param commentContent
 */
    public static void comments(String commentContent,Long orderId){
    	if("".equals(commentContent)){
    		return;
    	}
    	Orders order = Orders.findById(orderId);
    	
    	Users user =Contants.UserInfo.current();
    	Users ne = new Users();
    	Comment comment = new Comment();
    	comment.content = commentContent;
    	if(null!=user){
    		 try {
				BeanUtilsEx.copyProperties(ne, user);
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
    		comment.users =ne;
    	}
    	comment.commentTime = new Date();
    	order.comments.add(comment);
    	order.save();
    	renderJSON(comment);
    }
    /**
     * @param type
     * @param commnetId
     * 支持0，反对1
     */
    public static void supportOrOpposition(int type,long commnetId){
    	Comment comment = Comment.findById(commnetId);
    	if(type==0){
    		comment.supportNumber+=1;
    	}else if(type==1){
    		comment.oppositionNumber +=1;
    	}
    	comment.save();
    	renderJSON(comment);
    }
    /**
     * 加分0     减分 1
     */
    public static void changePoint(long orderId,int point,int type){
    	Orders order = Orders.findById(orderId);
    	if(type==0){
    		order.increase+=point;
    	}else if(type==1){
    		order.reduce+=point;
    	}
    	order.save();
//    	renderJSON(order);
    }
    public static void test(){
    	render("/Application/test.html");
    }
}
